import com.alibaba.fastjson2.JSON;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.*;
import java.util.HashMap;

public class excelUtils {

    //从表格中获取测试用例
    public static HashMap<String,Object> getTestCase(int index, String fileName) throws IOException {
        //读取xlsx文件
        InputStream inp = excelUtils.class.getClassLoader().getResourceAsStream(fileName);
        Workbook wb = WorkbookFactory.create(inp);
        //获取第一个工作表信息
        Sheet sheet = wb.getSheetAt(0);
        Row row = sheet.getRow(index);
        String params = row.getCell(11).getStringCellValue();
        String requestType = row.getCell(10).getStringCellValue();
        if(requestType.equalsIgnoreCase("POST")||
                requestType.equalsIgnoreCase("PUT")||
                requestType.equalsIgnoreCase("DELETE")){
            return JSON.parseObject(params);
        }
        else if(requestType.equalsIgnoreCase("GET")){
            HashMap<String,Object> hashMap = new HashMap<>();
            String[] strings = params.split("&");
            for(String str:strings){
                String[] kv = str.split("=");
                hashMap.put(kv[0],kv[1]);
            }
            return hashMap;
        }
        return null;
    }

    //从表格中获取json格式的预期结果
    public static String getExpectedResult(int index,String fileName) throws IOException {
        //读取xlsx文件
        InputStream inp = excelUtils.class.getClassLoader().getResourceAsStream(fileName);
        Workbook wb = WorkbookFactory.create(inp);
        //获取第一个工作表信息
        Sheet sheet = wb.getSheetAt(0);
        Row row = sheet.getRow(index);
        Cell cell = row.getCell(12);
        String result = cell.getStringCellValue();
        return result;
    }

}



